{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#Numpy（Numerical Python）是一个开源的Python科学计算库，用于快速处理任意维度的数组。",
   "id": "5f30ee61eaba8ee1"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 列表转化为ndarry",
   "id": "ae653731ed797b0b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:51:56.677020Z",
     "start_time": "2025-01-07T01:51:56.422847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "list1=[1,2,3,4,5]\n",
    "print(type(list1))\n",
    "print(list1)"
   ],
   "id": "c7f522bea5b7052f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "list每个元素之间有逗号，ndarray没有",
   "id": "1eb71674d786602f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:53:21.594690Z",
     "start_time": "2025-01-07T01:53:21.589522Z"
    }
   },
   "cell_type": "code",
   "source": [
    "oneArry=np.array(list1)\n",
    "print(type(oneArry))\n",
    "print(oneArry)"
   ],
   "id": "a2182703cdf9dc7c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray与python原生list运算效率对比",
   "id": "5325560651fda650"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T01:59:08.787722Z",
     "start_time": "2025-01-07T01:59:06.975639Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import time\n",
    "import random\n",
    "#随机生成10000000个元素\n",
    "a=[]\n",
    "for i in range(10000000):\n",
    "    a.append(random.random())\n",
    "print('生成完毕')\n",
    "b=np.array(a)\n",
    "print('转换完毕')"
   ],
   "id": "3bf443c2f1a1c217",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成完毕\n",
      "转换完毕\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:00:48.644643Z",
     "start_time": "2025-01-07T02:00:48.556408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=time.time()\n",
    "sum1=sum(a)\n",
    "t2=time.time()\n",
    "t3=time.time()\n",
    "sum2=np.sum(b)\n",
    "t4=time.time()\n",
    "print(t2-t1,t4-t3)"
   ],
   "id": "d6084d21eaa14fc2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0728452205657959 0.010964393615722656\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:06:23.561063Z",
     "start_time": "2025-01-07T02:06:23.555205Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1=type(range(10))\n",
    "print(t1)#range返回的是一个range对象，不能直接转换为ndarray\n",
    "t2=np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ],
   "id": "c461d57b7af2e13d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:09:20.157925Z",
     "start_time": "2025-01-07T02:09:20.152694Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t3=np.arange(0,10,2)#等价于range(0,10,2)，返回的是ndarray\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "id": "fcc8d03a8cc202aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:13:48.248583Z",
     "start_time": "2025-01-07T02:13:48.243225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#二维列表转ndarray\n",
    "list2=[[1,2],[3,4],[5,6]]\n",
    "twoArray=np.array(list2)\n",
    "print(type(twoArray))\n",
    "print(twoArray)\n",
    "print(list2)"
   ],
   "id": "a8e0ac367d47ee83",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1, 2], [3, 4], [5, 6]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:14:09.202373Z",
     "start_time": "2025-01-07T02:14:09.194454Z"
    }
   },
   "cell_type": "code",
   "source": "twoArray.tolist()",
   "id": "ae1d49945f209893",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4], [5, 6]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "ndarray的属性",
   "id": "be56eb266eadd47b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:18:53.419022Z",
     "start_time": "2025-01-07T02:18:53.414089Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#获取数组的维度\n",
    "print(twoArray.ndim)\n",
    "#形状\n",
    "print(twoArray.shape)\n",
    "#元素个数\n",
    "print(twoArray.size)\n",
    "#元素类型\n",
    "print(twoArray.dtype) #默认int为4字节"
   ],
   "id": "75e166d9106af3c5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 调整数组的形状",
   "id": "38286e73f3c98008"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:25:43.520461Z",
     "start_time": "2025-01-07T02:25:43.514991Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four=np.array([[1,2,3],[4,5,6]])\n",
    "print(four)\n",
    "four1=four\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "four.shape=(3,2)#改变形状\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "print(four)\n",
    "print(four1)"
   ],
   "id": "7ab7b52586b553a3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2350140553616\n",
      "2350140553616\n",
      "2350140553616\n",
      "2350140553616\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T02:31:34.004859Z",
     "start_time": "2025-01-07T02:31:34.000287Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four2=four.reshape(2,3)\n",
    "print(id(four))\n",
    "print(four)\n",
    "print(id(four2))"
   ],
   "id": "7a3b89a1ffae303",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2350140553616\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2350140554384\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:10:55.990879Z",
     "start_time": "2025-01-07T03:10:55.986296Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将多维变成一维\n",
    "five=four.reshape((6,),order='C')\n",
    "#默认情况下'C‘以行为主的顺序展开，'F'以列为主的顺序展开\n",
    "six=four.flatten()\n",
    "print(five)\n",
    "print(six)"
   ],
   "id": "30b811665c4ba4a3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:58.052695Z",
     "start_time": "2025-01-07T03:14:58.046550Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t=np.arange(24)\n",
    "print(t)\n",
    "print(f'shape:{t.shape}')\n",
    "print(f'ndim:{t.ndim}')\n",
    "t1=t.reshape(4,6)\n",
    "print(t1)\n",
    "print(f'shape:{t1.shape}')\n",
    "print(f'ndim:{t1.ndim}')"
   ],
   "id": "24f76c68db3335e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "shape:(24,)\n",
      "ndim:1\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "shape:(4, 6)\n",
      "ndim:2\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T03:16:26.652419Z",
     "start_time": "2025-01-07T03:16:26.647669Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#转成三维\n",
    "#最前面的是零轴，轴越小，越靠外，最小的轴是最外层\n",
    "#0轴，1轴，2轴\n",
    "t2=t.reshape(2,3,4)\n",
    "print(t2)\n",
    "print(f'shape:{t2.shape}')\n",
    "print(f'ndim:{t2.ndim}')"
   ],
   "id": "9d8281cb2cf44cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "shape:(2, 3, 4)\n",
      "ndim:3\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2表示轴0，3表示轴1，4表示轴2",
   "id": "614e187ed614f097"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的数据类型",
   "id": "84ad0a2e11f9edd2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:51:38.117999Z",
     "start_time": "2025-01-07T06:51:38.112224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f = np.array([1,2,3,4,5], dtype = np.int16)\n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "i=random.random()#生成0-1之间的随机数，尾数很长\n",
    "print(i)\n",
    "print(round(random.random(),2))\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))"
   ],
   "id": "93791b5204abba1f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "0.5147968257323329\n",
      "0.65\n",
      "[0.27 0.8  0.46 0.58 0.82 0.85 0.81 0.48 0.51 0.47]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组和数的运算",
   "id": "c060269f75790dbe"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T06:57:11.124756Z",
     "start_time": "2025-01-07T06:57:11.118790Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#列表不能和数进行运算，ndarray可以，这是一种广播机制\n",
    "t1 =np.arange(24).reshape((6,4))\n",
    "print(t1+2)\n",
    "print(\"-\"*20)\n",
    "print(t1*2)\n",
    "print(\"-\"*20)\n",
    "print(t1/2)"
   ],
   "id": "8a4931a778f49b38",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "同种形状的数组可以进行运算，不同形状的数组不能进行运算，有一个特例。\n",
    "行数或者列数相同的一维数组和多维数组可以进行计算："
   ],
   "id": "714d12772f4d8ff4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:03:05.157079Z",
     "start_time": "2025-01-07T07:03:05.152149Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(0,6)\n",
    "print(t1-t2)"
   ],
   "id": "e13ac2571a095792",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  0  0]\n",
      " [ 6  6  6  6  6  6]\n",
      " [12 12 12 12 12 12]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "结论：ndim要相同，某一个轴的size可以不同，但必须为1",
   "id": "f0a0de4b8941444b"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 练习轴",
   "id": "d8a980b1a2c398"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:25:29.985024Z",
     "start_time": "2025-01-07T07:25:29.976869Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "print(np.sum(a,axis=0)) # [5 7 9] 按那个轴进行求和，哪个轴就没了\n",
    "print(np.sum(a,axis = 1)) # [ 6 15]\n",
    "print(np.sum(a))\n",
    "a=np.arange(27).reshape((3,3,3))\n",
    "print(a)\n",
    "b=np.sum(a, axis=0)\n",
    "print(b.shape)\n",
    "print(\"-\"*20)\n",
    "print(b)\n",
    "c=np.sum(a, axis=1)\n",
    "print(c.shape)\n",
    "print(\"-\"*20)\n",
    "print(c)\n",
    "d=np.sum(a, axis=2)\n",
    "print(d.shape)\n",
    "print(\"-\"*20)\n",
    "print(d)"
   ],
   "id": "a63323e7454ee130",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "(3, 3)\n",
      "--------------------\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "(3, 3)\n",
      "--------------------\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "(3, 3)\n",
      "--------------------\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引和切片",
   "id": "d194821a8d0bd97f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:30:48.276714Z",
     "start_time": "2025-01-07T07:30:48.272224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a=np.arange(10)\n",
    "print(a[2],a)\n",
    "print(a[2:])"
   ],
   "id": "98fc7a4168252b69",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "多维数组的操作方法",
   "id": "1e524cf49578731f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:32:08.911582Z",
     "start_time": "2025-01-07T07:32:08.904876Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#切片的时候，行列用逗号隔开，行列的索引用冒号隔开,只取行的时候，逗号可以省略，但是取列的时候，逗号不能省,冒号运算符左闭右开\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "print(t1)\n",
    "print('*'*20)\n",
    "print(t1[1]) # 取一行(一行代表是一条数据，索引也是从0开始的) print(t1[1,:])取一行\n",
    "print('*'*20)\n",
    "print(t1[1:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[1:3,:])# 取连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3]])# 取不连续的多行\n",
    "print('*'*20)\n",
    "print(t1[[0,2,3],:])# 取不连续的多行\n",
    "print('*'*20)"
   ],
   "id": "4af7e2b7f4670a35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "对列进行操作",
   "id": "9caf4fe78c05fc9e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:43:09.275913Z",
     "start_time": "2025-01-07T07:43:09.269595Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(t1[:,1])# 取一列\n",
    "print('*'*20)\n",
    "print(t1[:,1:])# 连续的多列\n",
    "print('*'*20)\n",
    "print(t1[:,[0,2,3]])# 取不连续的多列\n",
    "print('*'*20)\n",
    "print(t1[2,3])# # 取某一个值,三行四列\n",
    "print('*'*20)\n",
    "print(t1[1:3,1:4])"
   ],
   "id": "27afad6ae31d09bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n",
      "[[ 7  8  9]\n",
      " [13 14 15]]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中的数值修改",
   "id": "e6365a662ed7b34c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "np支持批量广播修改,并且是在原数组上修改，防止内存过大",
   "id": "969cd739be140489"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:01:25.696535Z",
     "start_time": "2025-01-07T08:01:25.692001Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "print(t)\n",
    "# 修改某一行的值\n",
    "# t[1, :] = 0\n",
    "# 修改某一列的值\n",
    "# t[:, 1] = 0\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "# 修改连续多列\n",
    "# t[:, 1:4] = 0\n",
    "print(t)"
   ],
   "id": "1daa8cdc4c731e72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:04:21.749323Z",
     "start_time": "2025-01-07T08:04:21.744223Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "# # 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "# t[1:4,2:5]=0\n",
    "#\n",
    "# # 修改多个不相邻的点\n",
    "# t[[0,1],[0,3]]=0\n",
    "# print(t)\n",
    "# 可以根据条件修改，比如讲小于 10 的值改掉\n",
    "# t[t < 10] = 0\n",
    "# print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "# t[(t > 2) & (t < 6)] = 0 # 与\n",
    "# t[(t < 2) | (t > 6)] = 0 # 或\n",
    "# t[~(t > 6)] = 0 # 非\n",
    "# print(t)\n",
    "t=t.clip(10,18) #把小于10的改成10，大于18的改成18，梯度裁剪\n",
    "print(t)"
   ],
   "id": "ff223c1d04785937",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 三目运算",
   "id": "4a4b9c151d995861"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:10:36.531757Z",
     "start_time": "2025-01-07T08:10:36.524933Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange( 24 ).reshape( 4, 6 )\n",
    "t<10  #得到的是一个与t同等尺寸的布尔类型的数组"
   ],
   "id": "c57db92d04b290c8",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True, False, False],\n",
       "       [False, False, False, False, False, False],\n",
       "       [False, False, False, False, False, False]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:11:30.165304Z",
     "start_time": "2025-01-07T08:11:30.160256Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#python的三目运算\n",
    "a=10\n",
    "b=20\n",
    "c=a if a>b else b\n",
    "c"
   ],
   "id": "94f6c8af6a46123b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:13:47.071788Z",
     "start_time": "2025-01-07T08:13:47.065011Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array( [[80, 88], [82, 81], [75, 81]] )\n",
    "result = np.where( score > 80, True, False )\n",
    "print( result ) #np.where得到的是一个和array同等尺寸的数组，所以可以嵌套在score中使用\n",
    "score[result]=100\n",
    "score"
   ],
   "id": "b8b5d8406817c11c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 80, 100],\n",
       "       [100, 100],\n",
       "       [ 75, 100]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的添加删除和去重",
   "id": "a1e1dbe1d45ffa80"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "append操作",
   "id": "cfec30c29bea8f5b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:20:44.691482Z",
     "start_time": "2025-01-07T08:20:44.686587Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy.append [函数在数组的末尾添加值。] 追加操作会分配整个数组，并把原来的数组复制到新数组中\n",
    "#axis：默认为 None。当 axis 无定义时，是横向加成，返回总是为一维数组\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print(' 向 数 组 添 加 元 素 ：')\n",
    "print(np.append(a, [7, 8, 9]))#数组会被展平\n",
    "print('\\n')\n",
    "print(a) #进行append操作后，原数组不会改变"
   ],
   "id": "91282f462357d04b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:22:31.877698Z",
     "start_time": "2025-01-07T08:22:31.873602Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 0 添加元素：')#往哪个轴添加，哪个轴size就变大\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print('\\n')"
   ],
   "id": "ede2434eaa7fa8ed",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:25:10.736423Z",
     "start_time": "2025-01-07T08:25:10.731824Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 1 添加元素：') #注意添加是在数组的末尾添加\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))"
   ],
   "id": "df8c5f0ba2cb37f4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "insert操作",
   "id": "6be0307721f59c8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:29:29.759563Z",
     "start_time": "2025-01-07T08:29:29.753822Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数在给定索引之前，沿给定轴在输入数组中插入值。# 如果值的类型转换为要插入，则它与输入数组不同。\n",
    "# 插入没有原地的，函数会返回一个新数组。 此外，如果未提供轴，则输入数组会被展开。\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('\\n')"
   ],
   "id": "21bbe11d78bdf962",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 62
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:31:12.278650Z",
     "start_time": "2025-01-07T08:31:12.274330Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。')\n",
    "print('沿轴 0 广播：')\n",
    "print(np.insert(a, 1, [11], axis=0))#广播机制，写一个元素会自动进行填充,与轴0上的元素的格式进行匹配\n",
    "print('\\n')"
   ],
   "id": "fbd4282fe5818650",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "传递了 Axis 参数。 会广播值数组来配输入数组。\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 63
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T08:32:44.744447Z",
     "start_time": "2025-01-07T08:32:44.740193Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('沿轴 1 广播：')\n",
    "print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "af8b44a50f41af8e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数组中的删除",
   "id": "81ab28a49bdc9a07"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:42:28.788181Z",
     "start_time": "2025-01-07T09:42:28.781691Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样，\n",
    "# [如果未提供轴参数， 则输入数组将展开。]参 数 说 明 ： arr： 输 入 数 组obj：可以被切片，整数或者整数数组，表明要从输入数组删除的子数组 axis：沿着它删除给定子数组的轴，如果未提供，则输入数组会被展开\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print('第一个数组：')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。')\n",
    "print(np.delete(a,5))\n",
    "print('\\n')"
   ],
   "id": "b3ddf6e88ed5bf68",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 65
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:46:36.133927Z",
     "start_time": "2025-01-07T09:46:36.128586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('删除第一列：')\n",
    "print(np.delete(a,1,axis = 1))\n",
    "print('\\n')"
   ],
   "id": "c2d0ecf35cadbc65",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除第一列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:46:20.625073Z",
     "start_time": "2025-01-07T09:46:20.620615Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('删除第一行：')\n",
    "print(np.delete(a,1,axis = 0))\n",
    "print('\\n')"
   ],
   "id": "3d89148243be9ef3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除第一行：\n",
      "[[ 0  1  2  3]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "数组去重",
   "id": "f061768dae13388a"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "a = np.array([5,2,6,2,7,5,6,8,2,9])\n",
    "print ('第一个数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('第一个数组的去重值：')\n",
    "u = np.unique(a)\n",
    "print (u)\n",
    "print ('\\n')\n",
    "print ('去重数组的索引数组：')\n",
    "u,indices = np.unique(a, return_index = True)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "print ('我们可以看到每个和原数组下标对应的数值：')\n",
    "print (a)\n",
    "print ('去重数组的下标：')\n",
    "u,indices = np.unique(a,return_inverse = True)\n",
    "print (u)\n",
    "print (indices)\n",
    "print ('\\n')\n",
    "print ('返回去重元素的重复数量：')\n",
    "u,indices = np.unique(a,return_counts = True)\n",
    "print (u)\n",
    "print (indices)"
   ],
   "id": "10f49c80b71cf920"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "numpy的数学计算",
   "id": "6b05e495e452a90d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:48:39.450769Z",
     "start_time": "2025-01-07T12:48:39.442144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(result)\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score,axis=0)\n",
    "print(result)\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(result)\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score,axis=0)\n",
    "print(result)\n",
    "# 5. 数据的比较\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(result)\n",
    "result = np.minimum( [-2, -1, 0, 1, 2], 0 ) # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(result)\n",
    "result = np.maximum( [-2, -1, 0, 1, 2], [1, 2, 3, 4, 5] )\n",
    "print(result)\n",
    "# 接受的两个参数，也可以大小一致;第二个参数只是一个单独的值时，其实是用到了维度的广播机制；\n",
    "# 6. 求平均值\n",
    "result = np.mean(score) # 获取所有数据的平均值\n",
    "print(result)\n",
    "result = np.mean(score,axis=0) # 获取某一行或者某一列的平均值\n",
    "print(result)\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1,2,3], [4,5,6]])\n",
    "print(arr)\n",
    "print(arr.cumsum(0))"
   ],
   "id": "930a399cb43801e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "88\n",
      "[82 88]\n",
      "75\n",
      "[75 81]\n",
      "[0 0 0 1 2]\n",
      "[-2 -1  0  0  0]\n",
      "[1 2 3 4 5]\n",
      "81.16666666666667\n",
      "[79.         83.33333333]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#数组的拼接",
   "id": "55c7c6c288db850a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:06:51.936180Z",
     "start_time": "2025-01-07T10:06:51.930705Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 有的时候我们需要将两个数据加起来一起研究分析，我们就可以将其进行拼接然后分析\n",
    "import numpy as np\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1,2],[3,4]])\n",
    "b = np.array([[5,6],[7,8]])\n",
    "# 要求 a,b 两个数组的维度相同\n",
    "# print ('沿轴 0 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis= 0))#写哪个轴，哪个轴变化或消失\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.concatenate((a,b),axis =1))"
   ],
   "id": "fc90afb725e53595",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:40:43.139280Z",
     "start_time": "2025-01-07T10:40:43.133620Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠,堆叠后会增加维数\n",
    "#按哪个轴堆叠，就增加哪个轴，其余轴按照本来的形状一次排列。下面的例子中，按照轴1堆叠，则轴1变为2，因为有ab两个进行堆叠，其他的一次不变，就是2,2,3\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([[7,8,9],[10,11,12]])\n",
    "print ('沿轴 0 连接两个数组：')\n",
    "print (np.stack((a,b),axis= 0))\n",
    "print(np.stack((a,b),axis= 0).shape)\n",
    "print ('\\n')\n",
    "print ('沿轴 1 连接两个数组：')\n",
    "print (np.stack((a,b),axis = 1))\n",
    "print(np.stack((a,b),axis = 1).shape)"
   ],
   "id": "573a3f8716acb81c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴 0 连接两个数组：\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]]\n",
      "(2, 2, 3)\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组：\n",
      "[[[ 1  2  3]\n",
      "  [ 7  8  9]]\n",
      "\n",
      " [[ 4  5  6]\n",
      "  [10 11 12]]]\n",
      "(2, 2, 3)\n"
     ]
    }
   ],
   "execution_count": 74
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的分割",
   "id": "d6bf6fbeecccc75a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:36:05.811491Z",
     "start_time": "2025-01-07T11:36:05.804586Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "'''参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置\n",
    "（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为 0，横向切分。为 1 时，纵向切分\n",
    "'''\n",
    "arr = np.arange(9).reshape(3,3)\n",
    "print ('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr,3)\n",
    "print (b)"
   ],
   "id": "80d989262281f581",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n"
     ]
    }
   ],
   "execution_count": 75
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:40:48.737374Z",
     "start_time": "2025-01-07T11:40:48.731867Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.arange(12).reshape(4,3)\n",
    "print(arr)\n",
    "print ('将数组分为三个大小相等的子数组：')\n",
    "b = np.split(arr,3,axis=1)#按哪个轴，哪个轴发生变化\n",
    "print (b)#b中的每个元素都是4行一列，即1轴发生了变化,b还是一个ndarray类型的，是4（4,1）而不是一个列表\n",
    "print(b[0].shape)#(4,1)"
   ],
   "id": "72ccc05b8390b669",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "将数组分为三个大小相等的子数组：\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n",
      "(4, 1)\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中nan和inf",
   "id": "531fad7084fecf1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:46:11.273428Z",
     "start_time": "2025-01-07T11:46:11.267094Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF，最小的负整数是 0x8000 0000。\n",
    "查阅资料后，发现 inf 表示无穷大，需要使用 float(‘inf’) 函数来转化，那么对应的就有\n",
    "float('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现 inf 呢？ 比如一个数字除以 0，Python 中会报错，但是 numpy 中会是一个inf或者-inf\n",
    "另外还有 nan，这种写法在 pandans 中常见，表示缺失的数据，所以一般用 nan 来表示。\n",
    "任何与其做运算结果都是 nan（比如零除以零，在 numpy 中）\n",
    "'''\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a,type(a))\n",
    "print(b,type(b))"
   ],
   "id": "346d9b53ee456ded",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 81
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:48:06.118376Z",
     "start_time": "2025-01-07T11:48:06.113722Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.nan==np.nan)\n",
    "print(True==1)\n",
    "print(False==0)\n",
    "print(np.nan+1)"
   ],
   "id": "6f80f94232ad2ece",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n",
      "nan\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "统计数组中nan的个数",
   "id": "bba27661289aac63"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:50:28.913049Z",
     "start_time": "2025-01-07T11:50:28.906996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24,dtype=float).reshape(4,6)\n",
    "t[1,3] = np.nan\n",
    "print(t)"
   ],
   "id": "5278d1c03c27a15",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 83
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:53:42.734008Z",
     "start_time": "2025-01-07T11:53:42.729675Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#t！=t返回的是一个布尔类型的数组，True==1,False==0,此时用counzero就可以统计出nan的个数\n",
    "print(t!=t)\n",
    "print(np.count_nonzero(t!=t))"
   ],
   "id": "bf6143d757d95c2d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False  True False False]\n",
      " [False False False False False False]\n",
      " [False False False False False False]]\n",
      "1\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:16:21.379569Z",
     "start_time": "2025-01-07T13:16:21.374301Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#把数组中的nan用这一列的均值替换掉\n",
    "#先遍历每一列，看是否有nan\n",
    "for i in range(t.shape[1]):\n",
    "    #获取当前列的数据\n",
    "    temp_col=t[:,i]\n",
    "    #判断当前列是否有nan\n",
    "    nan_num=np.count_nonzero(temp_col!=temp_col)\n",
    "    #条件成立说明含有nan\n",
    "    if nan_num!=0:\n",
    "        #将这一列不为nan的元素拿出来，并计算均值\n",
    "        temp_col_not_nan=temp_col[temp_col==temp_col]\n",
    "        print(temp_col_not_nan)\n",
    "        #将nan替换成这一列的均值\n",
    "        temp_col[np.isnan(temp_col)]=np.mean(temp_col_not_nan)  \n",
    "print('='*50)\n",
    "print(t)"
   ],
   "id": "d38ebef8c501ef30",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==================================================\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# inf无穷大",
   "id": "a48940b1c06a5f48"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:22:49.914969Z",
     "start_time": "2025-01-07T13:22:49.908705Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.inf==np.inf)\n",
    "np.inf"
   ],
   "id": "712ed01e4bfe3e0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "inf"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:23:12.774555Z",
     "start_time": "2025-01-07T13:23:12.767979Z"
    }
   },
   "cell_type": "code",
   "source": "np.inf>-np.inf  #有正无穷大与负无穷大",
   "id": "26e21ba5125d5de4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 92
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 二维数组的转置与轴滚动",
   "id": "85874881ec1ee4d5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:35:56.318413Z",
     "start_time": "2025-01-07T13:35:56.312040Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a )\n",
    "print ('\\n')\n",
    "print ('对换数组：')\n",
    "print (np.transpose(a))  #转置返回的是一个新的数组，a不变\n",
    "# 与 transpose 一致\n",
    "a = np.arange(12).reshape(3,4)\n",
    "print ('原数组：')\n",
    "print (a)\n",
    "print ('\\n')\n",
    "print ('转置数组：')\n",
    "print (a.T)"
   ],
   "id": "ddd973f08d5b3c23",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "对换数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:36:12.734804Z",
     "start_time": "2025-01-07T13:36:12.729912Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4,6)\n",
    "re = t1.swapaxes(1,0)\n",
    "print (' 原 数 组 ：')\n",
    "print (t1)\n",
    "print ('\\n')\n",
    "print ('调用 swapaxes 函数后的数组：')\n",
    "print (re)"
   ],
   "id": "c7ee9357c2881973",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 94
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:40:16.523078Z",
     "start_time": "2025-01-07T13:40:16.518609Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#如果是3维及以上，称为轴交换\n",
    "t3=np.arange(60).reshape(3,4,5)\n",
    "print(t3.shape)\n",
    "print('-'*50)\n",
    "t3=np.swapaxes(t3,0,2)\n",
    "print(t3.shape)  #数组里面的数据不用记"
   ],
   "id": "dd26511eade95c44",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5)\n",
      "--------------------------------------------------\n",
      "(5, 4, 3)\n"
     ]
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 轴滚动",
   "id": "298671464761b226"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:50:41.015452Z",
     "start_time": "2025-01-07T13:50:41.009501Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#swapaxes函数每次只能交换两个轴，没有rollaxis函数方便\n",
    "a=np.ones((3,4,5,6))\n",
    "#函数的第二个参数指的是要滚动的轴，第三个参数是滚动到的位置，滚动的时候会滚动到这个位置之前，其余的轴保持相对位置不变\n",
    "b=np.rollaxis(a,3,1)\n",
    "print(b.shape)\n",
    "#如果要复原，第三个参数要写4，这样滚到4的前面才是3\n",
    "np.rollaxis(b,1,4).shape"
   ],
   "id": "36f2444ec67c6f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(3, 4, 5, 6)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 97
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy的注意点copy",
   "id": "e13c04bf5e36b350"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T14:01:15.129337Z",
     "start_time": "2025-01-07T14:01:15.124357Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. a=b 完全不复制，a和b相互影响\n",
    "# 2. a = b[:],视图的操作，一种切片，会创建新的对象a，但是a的数据完全由b保管，\n",
    "# 他们两个的数据变化是一致的\n",
    "# 3. a = b.copy(),复制，a和b互不影响\n",
    "b=[1,2,3,4,5]\n",
    "a=b[:]\n",
    "print(a)\n",
    "a[0]=0\n",
    "print(a)\n",
    "print(b)\n",
    "print('-'*50)\n",
    "b[2]=9\n",
    "print(a)\n",
    "print(b)"
   ],
   "id": "a1fe5c8f5b3370f9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[0, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4, 5]\n",
      "--------------------------------------------------\n",
      "[0, 2, 3, 4, 5]\n",
      "[1, 2, 9, 4, 5]\n"
     ]
    }
   ],
   "execution_count": 101
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "37a7c7988e55c96a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
